﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevelopTool.Bll.DbBlls;
using DevelopTool.Bll.ManageMenuBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonForms;
using DevelopTool.Forms.DbForms.ColumnForms;
using DevelopTool.Forms.DbForms.ExecuteForms;
using DevelopTool.Forms.DbForms.FunctionForms;
using DevelopTool.Forms.DbForms.JobForms;
using DevelopTool.Forms.DbForms.PackageForms;
using DevelopTool.Forms.DbForms.ProcedureForms;
using DevelopTool.Forms.DbForms.SequenceForms;
using DevelopTool.Forms.DbForms.SourceForms;
using DevelopTool.Forms.DbForms.SynonymForms;
using DevelopTool.Forms.DbForms.TableForms;
using DevelopTool.Forms.DbForms.TriggerForms;
using DevelopTool.Forms.DbForms.UsersForm;
using DevelopTool.Forms.DbForms.ViewForms;
using DevelopTool.Forms.DbInfoForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;
using DevelopTool.Model.Enums;
using Sunny.UI;

namespace DevelopTool.Forms.DbForms
{
    /// <summary>
    /// 数据库资源
    /// </summary>
    public partial class DbManageForm : BaseUIPage
    {
        private ManageMenuBll manageMenull;

        private DbInfoBll dbInfoBll;

        private DbManagerBll dbManagerBll;

        private DbSourceGroupBll dbSourceGroupBll;

        private Color controlForeColor = Color.White;
        public DbManageForm()
        {
            InitializeComponent();
        }

        private void DbManageForm_Load(object sender,EventArgs e)
        {
            this.manageMenull = new ManageMenuBll();
            this.dbInfoBll = new DbInfoBll();
            this.dbSourceGroupBll = new DbSourceGroupBll();

            this.manageControl.BaseBll = this.dbInfoBll;
            this.manageControl2.BaseBll = this.dbSourceGroupBll;

            UIMenuStyle uIMenuStyle = Setting.Theme.ControlThemColor;

            switch(uIMenuStyle)
            {
                case UIMenuStyle.Black: this.controlForeColor = Color.White; break;
                case UIMenuStyle.White: this.controlForeColor = Color.Black; break;
            }

            LoadManageControl();

            LoadManageControl2();
        }


        /// <summary>
        /// 加载管理页控件
        /// </summary>
        private void LoadManageControl()
        {
            List<ManageMenu> manageMenus = new List<ManageMenu>();
            LoadingHelper.ShowLoading("加载菜单数据",() =>
            {
                manageMenus = dbInfoBll.GetList().Cast<ManageMenu>().ToList();
            });

            this.manageControl.SetMenuData(manageMenus,"数据库连接列表");
        }

        /// <summary>
        /// 获取当前选中的DbInfo对象
        /// </summary>
        /// <returns></returns>
        private DbInfo GetSelectedDbInfo()
        {
            return dbInfoBll.GetById(this.manageControl.SelectedManageMenu.Id);
        }

        /// <summary>
        /// 菜单项添加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdded(object sender,MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;
            DbInfo dbInfo = dbInfoBll.GetById(e.ManageMenu.Id);

            this.dbSourceGroupBll.CreateDbSourceGroup(dbInfo);

            if(dbInfo != null && e.TreeNode.Name == "Node")
            {
                int symbol = 361788;
                switch(dbInfo.DbType)
                {
                    case Model.Enums.DbType.Oracle:
                        symbol = 62058;//oracle图标
                        break;
                }
                nav.SetNodeSymbol(e.TreeNode,symbol);
            }
        }


        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="text"></param>
        private void ManageControl_MenuQuery(object sender,string text)
        {
            Dictionary<string,List<ManageMenu>> dic = new Dictionary<string,List<ManageMenu>>();

            //关联到的节点名
            var nameList = manageMenull.GetIdListByName<DbInfo>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的数据库连接名",nameList);

            var timeList = manageMenull.GetIdListByTime<DbInfo>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的日期",timeList);

            var textList = dbInfoBll.GetListByContent(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的内容",textList);

            this.manageControl.SetMenuQueryData(dic);
        }


        /// <summary>
        /// 菜单节点单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void manageControl_MenuClick(object sender,MenuEventArgs e)
        {
            var treeNodeMouseClickEventArgs = sender as TreeNodeMouseClickEventArgs;

            if(treeNodeMouseClickEventArgs.Button == MouseButtons.Right)//鼠标右键
            {
                if(e.TreeNode.Name == "Node")
                {
                    e.TreeNode.ContextMenuStrip = this.contextMenuStripDbInfo;//绑定自定义菜单，不用控件预定义的
                    e.Cancel = true;
                }
            }
        }


        /// <summary>
        /// 菜单节点双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDoubleClick(object sender,MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;
            TreeNode parentTreeNode = e.TreeNode.Parent;
            if(parentTreeNode == null) return;
            DbInfo parentDbInfo = parentTreeNode.Tag as DbInfo;

            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                DbInfo dbInfo = dbInfoBll.GetById(e.ManageMenu.Id);

                if(e.TreeNode.Nodes.Count > 0)
                {
                    e.TreeNode.Expand();
                    return;
                }

                if(e.TreeNode.Name == "Node")//双击数据库连接
                {
                    this.dbManagerBll = new DbManagerBll(dbInfo);

                    bool connectOk = false;
                    LoadingHelper.ShowLoading("连接数据库",() =>
                    {
                        connectOk = this.dbManagerBll.CheckDbConnect();
                    });
                    if(!connectOk)
                    {
                        this.ShowErrorNotifier($"【{dbInfo.NodeName}】连接失败");
                        this.dbManagerBll = null;
                        return;
                    }

                    SetDbResouceNodes(nav,e.TreeNode,dbInfo,true,true,true);//添加数据库资源树节点集合
                    e.TreeNode.Expand();
                }
                else if(e.TreeNode.Name == "TDatabase")//双击数据库实例
                {

                }
                else if(e.TreeNode.Name == "TResTable")//双击表资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}表";
                    this.manageControl.ShowPageForm(new OracleTableForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TTable")//双击表
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}字段";
                    this.manageControl.ShowPageForm(new OracleTableColumnForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResTableColumn")//双击表字段资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}表字段";
                    this.manageControl.ShowPageForm(new OracleTableColumnForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResViewColumn")//双击视图字段资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}视图字段";
                    this.manageControl.ShowPageForm(new OracleViewColumnForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResView")//双击视图资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}视图";
                    this.manageControl.ShowPageForm(new OracleViewForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }

                else if(e.TreeNode.Name == "TResFunc")//双击函数资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}函数";
                    this.manageControl.ShowPageForm(new OracleFunctionForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResPackage")//双击函数资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}包";
                    this.manageControl.ShowPageForm(new OraclePackageForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResProcedure")//双击存储过程资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}存储过程";
                    this.manageControl.ShowPageForm(new OracleProcedureForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResTrigger")//双击触发器资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}触发器";
                    this.manageControl.ShowPageForm(new OracleTriggerForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResJob")//双击Job资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}Job";
                    this.manageControl.ShowPageForm(new OracleJobForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResSynonym")//双击同义词资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}同义词";
                    this.manageControl.ShowPageForm(new OracleSynonymForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResSequence")//双击序列资源
                {
                    string title = $"{dbInfo.NodeName}-{(string.IsNullOrEmpty(parentDbInfo.DataBase) ? string.Empty : $"{parentDbInfo.DataBase}-")}序列";
                    this.manageControl.ShowPageForm(new OracleSequenceForm(parentDbInfo,parentDbInfo.DataBase,""),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TResUsers")//双击用户资源
                {
                    string title = $"{dbInfo.NodeName}-所有用户";
                    this.manageControl.ShowPageForm(new OracleUsersForm(dbInfo),title,true,title + dbInfo.Id);
                }
                else if(e.TreeNode.Name == "TAllDatabase")//双击所有实例资源
                {
                    DataTable dataBaseNameAll = new DataTable();
                    LoadingHelper.ShowLoading("加载所有实例",() =>
                    {
                        dataBaseNameAll = this.dbManagerBll.GetAllDataBaseList();//获取所有数据库实例集合
                    });

                    foreach(DataRow row in dataBaseNameAll.Rows)
                    {
                        var tag = dbInfo.CloneObj();
                        tag.DataBase = row["DatabaseName"].ToString();
                        TreeNode dataBaseTreeNode = new TreeNode(tag.DataBase)
                        {
                            Name = "TDatabase",
                            Tag = tag
                        };

                        e.TreeNode.Nodes.Add(dataBaseTreeNode);//添加数据库到数据库信息节点
                        SetDbResouceNodes(nav,dataBaseTreeNode,tag);//添加数据库资源树节点集合
                        nav.SetNodeSymbol(dataBaseTreeNode,361888,20);
                    }
                }
                e.TreeNode.Expand();

            }
        }


        /// <summary>
        /// 获取数据库资源树节点
        /// </summary>
        /// <returns></returns>
        private void SetDbResouceNodes(UINavMenu nav,TreeNode parentNode,object tag,bool needDatabase = false,bool needJobs = true,bool needUsers = false)
        {
            List<TreeNode> dbResouceNodes = new List<TreeNode>() {
                    new TreeNode("表")
                {
                    Name = "TResTable",
                    Tag=tag
                },
                    new TreeNode("表字段")
                {
                    Name = "TResTableColumn",
                     Tag=tag
                },
                    new TreeNode("视图字段")
                {
                    Name = "TResViewColumn",
                     Tag=tag
                },
                new TreeNode("视图")
                {
                    Name = "TResView",
                     Tag=tag
                },
                new TreeNode("函数")
                {
                    Name = "TResFunc",
                     Tag=tag
                },
                new TreeNode("存储过程")
                {
                    Name = "TResProcedure",
                     Tag=tag
                },
                new TreeNode("触发器")
                {
                    Name = "TResTrigger",
                     Tag=tag
                },
                new TreeNode("同义词")
                {
                    Name = "TResSynonym",
                     Tag=tag
                },
                new TreeNode("包")
                {
                    Name = "TResPackage",
                     Tag=tag
                },
                new TreeNode("序列")
                {
                    Name = "TResSequence",
                     Tag=tag
                },
                new TreeNode("约束")
                {
                    Name = "TResConstraint",
                     Tag=tag
                },
                new TreeNode("索引")
                {
                    Name = "TResIndex",
                     Tag=tag
                },
                new TreeNode("数据库链接")
                {
                    Name = "TResDbLink",
                     Tag=tag
                },

            };

            parentNode.Nodes.AddRange(dbResouceNodes.ToArray());
            nav.SetNodeSymbol(dbResouceNodes[0],361646);
            nav.SetNodeSymbol(dbResouceNodes[1],361451);
            nav.SetNodeSymbol(dbResouceNodes[2],361451);
            nav.SetNodeSymbol(dbResouceNodes[3],362906);
            nav.SetNodeSymbol(dbResouceNodes[4],361874);
            nav.SetNodeSymbol(dbResouceNodes[5],363382);
            nav.SetNodeSymbol(dbResouceNodes[6],61988);
            nav.SetNodeSymbol(dbResouceNodes[7],61485);
            nav.SetNodeSymbol(dbResouceNodes[8],57477);
            nav.SetNodeSymbol(dbResouceNodes[9],361794);

            //需要添加Jobs节点
            if(needJobs)
            {
                var node = new TreeNode("Jobs")
                {
                    Name = "TResJob",
                    Tag = tag
                };
                parentNode.Nodes.Add(node);
                nav.SetNodeSymbol(node,61463);
            }

            //需要添加用户节点
            if(needUsers)
            {
                var node = new TreeNode("用户")
                {
                    Name = "TResUsers",
                    Tag = tag
                };
                parentNode.Nodes.Add(node);
                nav.SetNodeSymbol(node,61979);
            }

            //需要添加所有实例节点
            if(needDatabase)
            {
                var node = new TreeNode("所有实例")
                {
                    Name = "TAllDatabase",
                    Tag = tag
                };
                parentNode.Nodes.Add(node);
                nav.SetNodeSymbol(node,61672);
            }
        }


        /// <summary>
        /// 菜单节点拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDrag(object sender,MenuEventArgs e)
        {
            DbInfo dbInfo = manageMenull.GetExample<DbInfo>(e.ManageMenu);

            manageMenull.SimpleSave(dbInfo);
        }



        /// <summary>
        /// 删除菜单节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDelete(object sender,MenuEventArgs e)
        {
            DbInfo dbInfo = manageMenull.GetExample<DbInfo>(e.ManageMenu);

            if(dbInfoBll.Delete(dbInfo.Id))
            {
                this.ShowSuccessNotifier($"【{dbInfo.NodeName}】已删除");
            }
            else
            {
                e.Cancel = true;
                this.ShowWarningNotifier("不能删除非空目录");
            }
        }


        /// <summary>
        /// 重新加载菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuNeedReLoad(object sender,EventArgs e)
        {
            LoadManageControl();
        }




        /// <summary>
        /// 菜单节点重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuRename(object sender,MenuEventArgs e)
        {
            DbInfo dbInfo = manageMenull.GetExample<DbInfo>(e.ManageMenu);

            manageMenull.SimpleSave(dbInfo);
        }


        /// <summary>
        /// 新建菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdd(object sender,MenuEventArgs e)
        {
            DbInfo newDbInfo;

            //自定义创建节点
            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                OracleDbInfoForm oracleDbInfoForm = new OracleDbInfoForm(null);
                if(oracleDbInfoForm.ShowDialog() != DialogResult.OK)//弹出新建连接窗口
                {
                    e.Cancel = true;
                    return;
                }
                newDbInfo = oracleDbInfoForm.Tag as DbInfo;
                newDbInfo.NodePid = e.ManageMenu.NodePid;
                newDbInfo.NodeType = e.ManageMenu.NodeType;

            }
            else
            {
                newDbInfo = manageMenull.GetExample<DbInfo>(e.ManageMenu);
            }

            newDbInfo = manageMenull.SimpleSave(newDbInfo);

            e.ManageMenu = newDbInfo;

            this.dbSourceGroupBll.CreateDbSourceGroup(newDbInfo);

            this.ShowSuccessNotifier("新建成功");
        }

        #region 右键菜单事件


        private void 查询数据ToolStripMenuItem1_Click(object sender,EventArgs e)
        {
            string title = $"{this.manageControl.SelectedTreeNode.Text}-查询";
            DbInfo dbInfo = GetSelectedDbInfo();
            switch(dbInfo.DbType)
            {
                case Model.Enums.DbType.Oracle:

                    this.manageControl.ShowPageForm(new ExecuteSqlForm(dbInfo),title,false,title + dbInfo.Id);
                    break;
            }
        }


        private void 查看数据库资源ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            string title = $"{this.manageControl.SelectedTreeNode.Text}-资源";
            DbInfo dbInfo = GetSelectedDbInfo();
            switch(dbInfo.DbType)
            {
                case Model.Enums.DbType.Oracle:

                    this.manageControl.ShowPageForm(new OracleSourceForm(dbInfo),title,true,title + dbInfo.Id);
                    break;
            }
        }


        private void 编辑连接ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            bool isUpdate = false;
            DbInfo dbInfoNew = null;
            DbInfo dbInfo = GetSelectedDbInfo();
            switch(dbInfo.DbType)
            {
                case Model.Enums.DbType.Oracle:
                    OracleDbInfoForm oracleDbInfoForm = new OracleDbInfoForm(dbInfo);
                    DialogResult result3 = oracleDbInfoForm.ShowDialog();
                    if(result3 == DialogResult.OK)
                    {
                        //重新加载已修改的节点信息
                        isUpdate = true;
                        dbInfoNew = oracleDbInfoForm.Tag as DbInfo;
                    }
                    break;
            }

            if(isUpdate && dbInfoNew != null)
            {
                this.manageControl.SelectedTreeNode.Text = dbInfoNew.NodeName;
            }
        }

        private void 复制连接ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            DbInfo newDbInfo = GetSelectedDbInfo().CloneObj();
            newDbInfo.Id = 0;
            newDbInfo.NodeName += "-复制";
            //保存至数据库
            newDbInfo = manageMenull.SimpleSave(newDbInfo);

            if(newDbInfo != null && newDbInfo.Id > 0)
            {
                int symbol = 61642;
                switch(newDbInfo.DbType)
                {
                    case Model.Enums.DbType.Oracle:
                        symbol = 62058;//oracle图标
                        break;
                }

                TreeNode treeNode = this.manageControl.NavMenuLeft.CreateChildNode(this.manageControl.SelectedTreeNode.Parent,newDbInfo.NodeName,symbol,24,new Random().Next());
                treeNode.Tag = newDbInfo;
                treeNode.Name = "Node";

                this.dbSourceGroupBll.CreateDbSourceGroup(newDbInfo);
            }
        }


        private void 删除连接ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            if(this.ShowAskDialog($"{GlobalData.AppName_zh}-重要提示", $"确认删除【{this.manageControl.SelectedTreeNode.Text}】吗？", UIStyle.Orange))
            {
                if(dbInfoBll.Delete(this.manageControl.SelectedManageMenu.Id))
                {
                    this.manageControl.SelectedTreeNode.Remove();
                    this.ShowInfoNotifier("删除成功");
                }
            }
        }

        private void 属性ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            ShowAttributeForm showAttributeForm = new ShowAttributeForm(this.manageControl.SelectedManageMenu.Id);
            showAttributeForm.BaseBll = this.dbInfoBll;
            showAttributeForm.ShowDialog(this);
        }
        #endregion


        #region 分组管理

        private void LoadManageControl2()
        {
            List<ManageMenu> manageMenus = new List<ManageMenu>();
            LoadingHelper.ShowLoading("加载菜单数据",() =>
            {
                manageMenus = dbSourceGroupBll.GetList().Cast<ManageMenu>().ToList();
            });

            this.manageControl2.SetMenuData(manageMenus,"资源分组列表");
        }
        private void manageControl2_MenuNeedReLoad(object sender,EventArgs e)
        {
            LoadManageControl2();
        }


        private void manageControl2_MenuAdded(object sender,MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;
            DbSourceGroup dbSourceGroup = dbSourceGroupBll.GetById(e.ManageMenu.Id);

            if(dbSourceGroup != null)
            {
                if(dbSourceGroup.SourceType == "TDbInfo")
                {
                    int symbol = 361788;
                    switch(dbSourceGroup.DbInfo.DbType)
                    {
                        case Model.Enums.DbType.Oracle:
                            symbol = 62058;//oracle图标
                            break;
                    }
                    nav.SetNodeSymbol(e.TreeNode,symbol);
                }
                else if(dbSourceGroup.SourceType == "TResTable")
                {
                    nav.SetNodeSymbol(e.TreeNode,361646);
                    nav.SetNodeTipsText(e.TreeNode," 表 ",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResTableColumn")
                {
                    nav.SetNodeSymbol(e.TreeNode,361451);
                    nav.SetNodeTipsText(e.TreeNode,"字段",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResViewColumn")
                {
                    nav.SetNodeSymbol(e.TreeNode,361451);
                    nav.SetNodeTipsText(e.TreeNode,"字段",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResView")
                {
                    nav.SetNodeSymbol(e.TreeNode,362906);
                    nav.SetNodeTipsText(e.TreeNode,"视图",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResFunc")
                {
                    nav.SetNodeSymbol(e.TreeNode,361874);
                    nav.SetNodeTipsText(e.TreeNode,"函数",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResProcedure")
                {
                    nav.SetNodeSymbol(e.TreeNode,363382);
                    nav.SetNodeTipsText(e.TreeNode,"过程",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResTrigger")
                {
                    nav.SetNodeSymbol(e.TreeNode,61988);
                    nav.SetNodeTipsText(e.TreeNode,"触发",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResSynonym")
                {
                    nav.SetNodeSymbol(e.TreeNode,61485);
                    nav.SetNodeTipsText(e.TreeNode,"同义",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResPackage")
                {
                    nav.SetNodeSymbol(e.TreeNode,57477);
                    nav.SetNodeTipsText(e.TreeNode," 包 ",nav.FillColor,this.controlForeColor);
                }
                else if(dbSourceGroup.SourceType == "TResSequence")
                {
                    nav.SetNodeSymbol(e.TreeNode,361794);
                    nav.SetNodeTipsText(e.TreeNode,"序列",nav.FillColor,this.controlForeColor);
                }
            }
        }


        private void manageControl2_MenuClick(object sender,MenuEventArgs e)
        {
            var treeNodeMouseClickEventArgs = sender as TreeNodeMouseClickEventArgs;

            if(treeNodeMouseClickEventArgs.Button == MouseButtons.Right)//鼠标右键
            {
                if(e.TreeNode.Name == "Folder")
                {
                    if(e.ManageMenu.NodePid == 0)
                    {
                        e.TreeNode.ContextMenuStrip = this.contextMenuStripGroup;//绑定自定义菜单，不用控件预定义的
                    }
                    else
                    {
                        e.TreeNode.ContextMenuStrip = this.contextMenuStripGroupFolder;//绑定自定义菜单，不用控件预定义的
                    }
                }
                else if(e.TreeNode.Name == "Node")
                {
                    e.TreeNode.ContextMenuStrip = this.contextMenuStripGroupSource;
                }
                else if(e.TreeNode.Name == "TopNode")
                {
                    e.TreeNode.ContextMenuStrip = null;
                }

                e.Cancel = true;
            }
        }


        private void 新建分组ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            string groupName = string.Empty;
            if(this.ShowInputStringDialog(ref groupName,true,"请输入分组名称"))
            {
                DbSourceGroup parentDbSourceGroup = this.dbSourceGroupBll.GetById(this.manageControl2.SelectedManageMenu.Id);

                DbSourceGroup dbSourceGroup = new DbSourceGroup()
                {
                    NodePid = this.manageControl2.SelectedManageMenu.Id,
                    NodeName = groupName,
                    NodeType = MenuNodeType.Folder,
                    DbInfo = parentDbSourceGroup.DbInfo,
                    SourceType = "TGroupFolder"
                };
                dbSourceGroup = this.dbSourceGroupBll.Save(dbSourceGroup);

                TreeNode newTreeNode = this.manageControl2.NavMenuLeft.CreateChildNode(this.manageControl2.SelectedTreeNode,dbSourceGroup.NodeName,new Random().Next());
                newTreeNode.Tag = dbSourceGroup;
                newTreeNode.Name = "Folder";

                this.manageControl2.NavMenuLeft.SetNodeSymbol(newTreeNode,61564);//设置节点图标

                this.manageControl2.SelectedTreeNode.Expand();

                this.manageControl2.NavMenuLeft.SelectedNode = newTreeNode;//选中新建的节点

            }
        }



        private void 重命名分组ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            string groupName = this.manageControl2.SelectedTreeNode.Text;
            if(this.ShowInputStringDialog(ref groupName,true,"请输入新分组名称"))
            {
                DbSourceGroup dbSourceGroup = manageMenull.GetExample<DbSourceGroup>(this.manageControl2.SelectedManageMenu);

                dbSourceGroup.NodeName = groupName;
                this.manageMenull.SimpleSave(dbSourceGroup);

                this.manageControl2.SelectedTreeNode.Text = groupName;
            }

        }


        private void 删除分组ToolStripMenuItem_Click(object sender,EventArgs e)
        {

            if(this.ShowAskDialog($"{GlobalData.AppName_zh}-重要提示", "是否确定删除分组？", UIStyle.Orange))
            {
                this.dbSourceGroupBll.DeleteAll(this.manageControl2.SelectedManageMenu.Id);
                if(this.dbSourceGroupBll.Delete(this.manageControl2.SelectedManageMenu.Id))
                {
                    this.manageControl2.SelectedTreeNode.Remove();
                    this.ShowSuccessNotifier("删除成功");
                }
            }
        }


        private void 属性ToolStripMenuItem1_Click(object sender,EventArgs e)
        {
            ShowAttributeForm showAttributeForm = new ShowAttributeForm(this.manageControl2.SelectedManageMenu.Id);
            showAttributeForm.BaseBll = this.dbSourceGroupBll;
            showAttributeForm.ShowDialog(this);
        }


        private void 删除资源ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            if(this.ShowAskDialog($"{GlobalData.AppName_zh}-重要提示",$"是否确定删除资源{this.manageControl2.SelectedManageMenu.NodeName}？", UIStyle.Orange))
            {
                if(this.dbSourceGroupBll.Delete(this.manageControl2.SelectedManageMenu.Id))
                {
                    this.manageControl2.SelectedTreeNode.Remove();
                    this.ShowSuccessNotifier("删除成功");
                }
            }
        }


        private void manageControl2_MenuAdd(object sender,MenuEventArgs e)
        {
            if(e.ManageMenu.NodeType == MenuNodeType.Folder)
            {
                string groupName = string.Empty;
                if(this.ShowInputStringDialog(ref groupName,true,"请输入分组名称"))
                {
                    DbSourceGroup dbSourceGroup = manageMenull.GetExample<DbSourceGroup>(e.ManageMenu);
                    dbSourceGroup.NodeName = groupName;
                    DbSourceGroup parentDbSourceGroup = this.dbSourceGroupBll.GetById(this.manageControl2.SelectedManageMenu.Id);
                    dbSourceGroup.DbInfo = parentDbSourceGroup.DbInfo;
                    dbSourceGroup = manageMenull.SimpleSave(dbSourceGroup);

                    e.ManageMenu = dbSourceGroup;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void manageControl2_MenuDelete(object sender,MenuEventArgs e)
        {
            if(dbSourceGroupBll.Delete(e.ManageMenu.Id))
            {
                this.ShowSuccessNotifier($"【{e.ManageMenu.NodeName}】已删除");
            }
            else
            {
                e.Cancel = true;
                this.ShowWarningNotifier("不能删除非空分组");
            }
        }


        private void manageControl2_MenuRename(object sender,MenuEventArgs e)
        {
            DbSourceGroup dbSourceGroup = manageMenull.GetExample<DbSourceGroup>(e.ManageMenu);

            manageMenull.SimpleSave(dbSourceGroup);
        }

        private void manageControl2_MenuDrag(object sender,MenuEventArgs e)
        {
            DbSourceGroup dbSourceGroup = manageMenull.GetExample<DbSourceGroup>(e.ManageMenu);

            manageMenull.SimpleSave(dbSourceGroup);
        }


        private void manageControl2_MenuQuery(object sender,string text)
        {
            Dictionary<string,List<ManageMenu>> dic = new Dictionary<string,List<ManageMenu>>();

            var nameList = manageMenull.GetIdListByName<DbSourceGroup>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的名称",nameList);

            var timeList = manageMenull.GetIdListByTime<DbSourceGroup>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的日期",timeList);

            var textList = dbSourceGroupBll.GetListByText(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的内容",textList);

            this.manageControl2.SetMenuQueryData(dic);
        }


        private void manageControl2_MenuDoubleClick(object sender,MenuEventArgs e)
        {
            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                DbSourceGroup dbSourceGroup = dbSourceGroupBll.GetById(e.ManageMenu.Id);
                if(dbSourceGroup == null) return;

                if(dbSourceGroup.SourceType == "TResTable")
                {
                    this.manageControl2.ShowPageForm(new OracleTableColumnForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName),$"{dbSourceGroup.NodeName}-字段",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResTableColumn")
                {
                    this.manageControl2.ShowPageForm(new OracleTableColumnForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.TableName,dbSourceGroup.NodeName),$"{dbSourceGroup.TableName}.{dbSourceGroup.NodeName}-表字段",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResViewColumn")
                {
                    this.manageControl2.ShowPageForm(new OracleViewColumnForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.TableName,dbSourceGroup.NodeName),$"{dbSourceGroup.TableName}.{dbSourceGroup.NodeName}-视图字段",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResView")
                {
                    OracleViewForm oracleViewForm = new OracleViewForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oracleViewForm.FixedFirstRowColumnName = "TEXT";
                    this.manageControl2.ShowPageForm(oracleViewForm,$"{dbSourceGroup.NodeName}-视图",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResFunc")
                {
                    OracleFunctionForm oracleFunctionForm = new OracleFunctionForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oracleFunctionForm.FixedFirstRowColumnName = "TEXT";
                    this.manageControl2.ShowPageForm(oracleFunctionForm,$"{dbSourceGroup.NodeName}-函数",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResProcedure")
                {
                    OracleProcedureForm oracleProcedureForm = new OracleProcedureForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oracleProcedureForm.FixedFirstRowColumnName = "TEXT";
                    this.manageControl2.ShowPageForm(oracleProcedureForm,$"{dbSourceGroup.NodeName}-存储过程",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResTrigger")
                {
                    OracleTriggerForm oracleTriggerForm = new OracleTriggerForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oracleTriggerForm.FixedFirstRowColumnName = "TRIGGER_BODY";
                    this.manageControl2.ShowPageForm(oracleTriggerForm,$"{dbSourceGroup.NodeName}-触发器",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResSynonym")
                {
                    OracleSynonymForm oracleSynonymForm = new OracleSynonymForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oracleSynonymForm.FixedFirstRowColumnName = "SYNONYM_NAME";
                    this.manageControl2.ShowPageForm(oracleSynonymForm,$"{dbSourceGroup.NodeName}-同义词",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResPackage")
                {
                    OraclePackageForm oraclePackageForm = new OraclePackageForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oraclePackageForm.FixedFirstRowColumnName = "PACKAGEBODY_TEXT";
                    this.manageControl2.ShowPageForm(oraclePackageForm,$"{dbSourceGroup.NodeName}-包",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }
                else if(dbSourceGroup.SourceType == "TResSequence")
                {
                    OracleSequenceForm oracleSequenceForm = new OracleSequenceForm(dbSourceGroup.DbInfo,dbSourceGroup.DataBaseName,dbSourceGroup.NodeName);
                    oracleSequenceForm.FixedFirstRowColumnName = "SEQUENCE_NAME";
                    this.manageControl2.ShowPageForm(oracleSequenceForm,$"{dbSourceGroup.NodeName}-序列",true,dbSourceGroup.NodeName + dbSourceGroup.Id);
                }

            }
        }
        #endregion 分组管理


    }
}
